package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.bean.MyFileBean;
import flc.ast.databinding.ActivityFolderBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class FolderActivity extends BaseAc<ActivityFolderBinding> {

    public static String folderName;
    private FileAdapter fileAdapter;
    private List<MyFileBean> listShow = new ArrayList<>();
    private boolean isEdit;
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_folder;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            isEdit = false;
            fileAdapter.setEdit(isEdit);
            getData();
            mDataBinding.llFolderBottom.setVisibility(View.GONE);
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivFolderBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivEdit.setOnClickListener(this);
        mDataBinding.ivAdd.setOnClickListener(this);
        mDataBinding.ivRemove.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new FileAdapter();
        fileAdapter.setEdit(isEdit);
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivEdit://编辑
                if (isEdit) return;
                isEdit = true;
                fileAdapter.setEdit(isEdit);
                fileAdapter.notifyDataSetChanged();
                listSel.clear();
                mDataBinding.llFolderBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.ivAdd://添加文件
                startActivity(MyFileActivity.class);
                break;
            case R.id.ivRemove://移除
            case R.id.ivDelete://删除
                delete();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (fileAdapter.getItem(position).isSelected()) {
                fileAdapter.getItem(position).setSelected(false);
                listSel.remove(fileAdapter.getItem(position).getPath());
            } else {
                fileAdapter.getItem(position).setSelected(true);
                listSel.add(fileAdapter.getItem(position).getPath());
            }
            fileAdapter.notifyItemChanged(position);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc(getString(R.string.get_all_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(MyFileActivity.class);
                    }
                })
                .request();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacy + "/" + folderName;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyFileBean(file.getPath()));
            }
        }
        mDataBinding.tvFolderTitle.setText(folderName);
        if (listShow.size() > 0) {
            fileAdapter.setList(listShow);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.tvFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        for (String path : listSel) {
                            FileUtils.delete(path);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                    }
                });
            }
        }, 1000);
    }


}